package com.hsy.crm.web.service.asyn;

import com.hsy.crm.web.domain.cm.App;
import com.hsy.crm.web.domain.entity.Message;
import com.hsy.crm.web.domain.entity.StoreProductDetail;
import com.hsy.crm.web.domain.ex.ServiceException;
import com.hsy.crm.web.service.asyn.inter.IOrderService;
import com.hsy.crm.web.service.asyn.marketprofit.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MarketProfitService {

    @Autowired
    AsynOrderService asynOrderService;

    @Autowired
    AsynStoreService asynStoreService;

    @Autowired
    BuyBackProfitService buyBackProfitService;

    @Autowired
    BuyProfitService buyProfitService;

    @Autowired
    OtherInProfitService otherInProfitService;

    @Autowired
    OtherOutProfitService otherOutProfitService;

    @Autowired
    SaleBackProfitService saleBackProfitService;

    @Autowired
    SaleProfitService saleProfitService;

    @Autowired
    UnBuyBackProfitService unBuyBackProfitService;

    @Autowired
    UnBuyProfitService unBuyProfitService;

    @Autowired
    UnOtherInProfitService unOtherInProfitService;

    @Autowired
    UnOtherOutProfitService unOtherOutProfitService;

    @Autowired
    UnSaleBackProfitService unSaleBackProfitService;

    @Autowired
    UnSaleProfitService unSaleProfitService;


    /**
     *
     * 非仓库相关出入库情况汇总
     * 入库：
     *      采购               |  计算未计算的销售明细
     *      采购退货反审核     |  虚拟重复入库
     *                              1 指定采购单         等同于正常采购
     *                              2 未指定采购单       等同于正常采购
     *      销售退货           |  虚拟重复入库
     *                              1 指定销售单         取销售明细成本
     *                              2 未指定销售单       取最后一次销售明细成本
     *      销售反审核         |  重复入库
     *                              该销售明细利润数据重置  再处理未处理的销售明细
     * 出库：
     *      销售               |  处理利润
     *      销售退货反审核     |  虚拟重复出库
     *                              1 指定销售单         取对应的销售退货成本
     *                              2 未指定销售单       取对应的销售退货成本
     *      采购退货           |  虚拟重复出库
     *                              1 指定采购单         同采购反审核
     *                              2 未指定采购单       while 最后一次未销售的采购明细出库  没有数据则不处理（等待入库时处理）
     *      采购反审核         |  重复出库
     *                              如果采购明细有销售  则该库存利润全部重算
     *
     * 仓库相关出入库情况汇总
     *      调拨
     *      拆装
     *      盘点
     *      其它入库
     *      其它出库
     *
     *
     * 采购单 退回状态不能编辑
     *
     * 指定销售单 销售退货  退货金额必须一致
     *
     *
     * test start
     *
     * 采购3   销售4   采购1
     *
     * test end
     *
     * @param message
     */
    @Transactional
    public void calc(Message message){

        IOrderService service = this.getService(message);

        // store product,store product detail
        List<StoreProductDetail> storeProductDetails = service.addStock(message);

        // relation money
        service.relationAccount(message);

        // update last product  sale or buy price

        // market profit
        this.marketProfit(message,storeProductDetails);

        // profit
    }

    private void marketProfit(Message message,List<StoreProductDetail> storeProductDetails){
        int actionType = message.getActionType();
        if( actionType != App.ExamType.EXAMINE && actionType != App.ExamType.INVALID ){
            throw new ServiceException("action type error");
        }
        switch (message.getTargetType()){
            //销售    --1
            case App.BILL_TYPE.ORDER_SHD:
                if( actionType == App.ExamType.EXAMINE ){
                    saleProfitService.init(message,storeProductDetails);
                }else{
                    unSaleProfitService.init(message,storeProductDetails);
                }
                break;
            //采购    --1
            case App.BILL_TYPE.ORDER_JHD:
                if( actionType == App.ExamType.EXAMINE ){
                    buyProfitService.init(message,storeProductDetails);
                }else{
                    unBuyProfitService.init(message,storeProductDetails);
                }
                break;
            //销售退货
            case App.BILL_TYPE.ORDER_SHTHD:
                if( actionType == App.ExamType.EXAMINE ){
                    saleBackProfitService.init(message,storeProductDetails);
                }else{
                    unSaleBackProfitService.init(message,storeProductDetails);
                }
                break;
            //采购退货
            case App.BILL_TYPE.ORDER_JHTHD:
                if( actionType == App.ExamType.EXAMINE ){
                    buyBackProfitService.init(message,storeProductDetails);
                }else{
                    unBuyBackProfitService.init(message,storeProductDetails);
                }
                break;
            //调拨
            case App.BILL_TYPE.ORDER_DB:
                this.storeProfitMgr(message,storeProductDetails);
                break;
            //拆装
            case App.BILL_TYPE.ORDER_CZ:
                this.storeProfitMgr(message,storeProductDetails);
                break;
            //盘点
            case App.BILL_TYPE.ORDER_PD:
                this.storeProfitMgr(message,storeProductDetails);
                break;
            //其它入库/其它出库
            case App.BILL_TYPE.ORDER_QTRKD:
                this.storeProfitMgr(message,storeProductDetails);
                break;
            case App.BILL_TYPE.ORDER_QTCKD:
                this.storeProfitMgr(message,storeProductDetails);
                break;
            default:
                break;
        }
    }

    /**
     * 仓库相关利润计算
     * @param message
     * @param storeProductDetails
     */
    private void storeProfitMgr(Message message,List<StoreProductDetail> storeProductDetails){
        int actionType = message.getActionType();
        for( StoreProductDetail storeProductDetail : storeProductDetails ){
            if( actionType == App.ExamType.EXAMINE ){
                if( storeProductDetail.getType() == App.StoreType.IN ){
                    otherInProfitService.init(message,storeProductDetail);
                }else if( storeProductDetail.getType() == App.StoreType.OUT ){
                    otherOutProfitService.init(message,storeProductDetail);
                }else{
                    throw new ServiceException("storeProductDetail in or out type error1");
                }
            }else{
                if( storeProductDetail.getType() == App.StoreType.IN ){
                    unOtherInProfitService.init(message,storeProductDetail);
                }else if( storeProductDetail.getType() == App.StoreType.OUT ){
                    unOtherOutProfitService.init(message,storeProductDetail);
                }else{
                    throw new ServiceException("storeProductDetail in or out type error2");
                }
            }
        }
    }

    private IOrderService getService(Message message){
        IOrderService service = null;
        switch (message.getTargetType()){
            //售货
            case App.BILL_TYPE.ORDER_SHD:
                service = asynOrderService;
                break;
            //进货
            case App.BILL_TYPE.ORDER_JHD:
                service = asynOrderService;
                break;
            //售货退货
            case App.BILL_TYPE.ORDER_SHTHD:
                service = asynOrderService;
                break;
            //进货退货
            case App.BILL_TYPE.ORDER_JHTHD:
                service = asynOrderService;
                break;
            //调拨
            case App.BILL_TYPE.ORDER_DB:
                service = asynStoreService;
                break;
            //拆装
            case App.BILL_TYPE.ORDER_CZ:
                service = asynStoreService;
                break;
            //盘点
            case App.BILL_TYPE.ORDER_PD:
                service = asynStoreService;
                break;
            //其它入库/其它出库
            case App.BILL_TYPE.ORDER_QTRKD:
                service = asynStoreService;
                break;
            //其它入库/其它出库
            case App.BILL_TYPE.ORDER_QTCKD:
                service = asynStoreService;
                break;
            default:
                break;
        }
        return service;
    }

}
